﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.SpecialUtils.View;
using CatUtilsDll.Utils.Currency.WebUtils;
using RabbitDownload.Helper;

using System.Collections.Generic;
using System.Net;

namespace RabbitDownload.MangerTool
{
    internal class DownloadControlTool
    {
        /// <summary>
        /// 下载文件夹下所有
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="webHeaderCollection"></param>
        public void DownloadFloder(string FloderPath)
        {
            foreach (string SingleFile in FloderNameHelper.GetFullTextList(FloderPath))
            {
                DownListFile(SingleFile);
                FileHelper.FileMoveToFloder(SingleFile, FloderNameHelper.CreateChildFloderPath(SingleFile, "已下载列表"));
            }
        }

        /// <summary>
        /// 下载列表文件
        /// </summary>
        /// <param name="FilePath"></param>

        public void DownListFile(string FilePath)
        {
            List<string> FileList = FileHelper.ReadAllLine(FilePath);
            string GetFileName = TextHelper.ReplaceBadChar(FileNameHelper.GetFileNameWithoutExtension(FilePath));
            if (GetFileName == "")
            {
                System.Console.WriteLine("文件名混乱，无法下载");
                return;
            }
            string DwonloadFloder =
             FloderNameHelper.CreateChildFloderPath(FloderNameHelper.CreateChildFloderPath(FilePath, "已下载文件"), GetFileName);
            if (!FloderHelper.FloderExits(DwonloadFloder, true))
            {
                System.Console.WriteLine("无法创建文件夹，放弃");
                return;
            }
            int CompltedCount = 0;
            new FileControlTool().CopyPropaganda(DwonloadFloder, true);

            foreach (string FileUrl in FileList)
            {
                string TempLink = FileUrl;
                new PrintLog().PercentLog(FileList.Count, ++CompltedCount);
                //  TempLink = "https://pixiv.re/"+TextHelper.ReplaceTextEnhance(FileNameHelper.GetFileNameWithoutExtension(TempLink), "_p[0-9]{1}", "")+".jpg";
                System.Console.WriteLine(TempLink);
                WebHelper.DownloadToFloder(TempLink, DwonloadFloder, GetWebHeader(FileUrl));
            }
        }

        private WebHeaderCollection GetWebHeader(string ImgLink)
        {
            WebHeaderCollection webHeaderCollection = WebHelper.PcWebHeader();
            if (ImgLink.IndexOf("i.pximg.net") >= 0)
                webHeaderCollection.Add("Referer", "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=123222");
            return webHeaderCollection;
        }

        /// <summary>
        ///  切割列表文件的文件夹
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="SkipText"></param>
        public void SkipListFloder(string FloderPath, string SkipText)
        {
            int CompltedCount = 0;
            List<string> FileList = FloderNameHelper.GetFullTextList(FloderPath);
            foreach (string SingleFile in FileList)
            {
                new PrintLog().PercentLog(FileList.Count, ++CompltedCount);
                SkipListFile(SingleFile, SkipText);
            }
        }

        /// <summary>
        /// 切割单个列表
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="SkipText"></param>
        public void SkipListFile(string FilePath, string SkipText)
        {
            int SkipCount = TextHelper.StringToInt(SkipText);
            if (SkipCount <= 0)
            {
                MessageHelper.Alert("数量不正确");

                return;
            }
            int FileCount = 1;
            string NewFilePath = FileNameHelper.CreateFileNameWithChildFloder(FilePath, "切割列表");
            foreach (List<string> SkipList in ListHelper.ListSkip(FileHelper.ReadAllLine(FilePath), SkipCount))
            {
                FileHelper.WriteList(FileNameHelper.CreateFileNameAddTip(NewFilePath, string.Format("已切割{0}", FileCount++)), SkipList);
            }
            System.Console.WriteLine("切割完毕。");
        }
    }
}